/*
 *
 *
 */
package com.wbs.shop.nuby.view.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EdgeEffect;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.view.CBLoopViewPager;
import com.bumptech.glide.Glide;
import com.wbs.shop.nuby.R;
import com.wbs.shop.nuby.api.ApiHelper;
import com.wbs.shop.nuby.commom.Key;
import com.wbs.shop.nuby.view.base.BaseActivity;
import com.wbs.shop.nuby.view.listener.SimplPageChangeListener;
import com.yanzhenjie.sofia.Utils;

import java.lang.reflect.Field;
import java.util.ArrayList;

import butterknife.BindView;


/**
 * Author:  zhoucl
 * Description:
 * Date:
 */
public class GuideActivity extends BaseActivity {

    @BindView(R.id.convenientBanner)
    ConvenientBanner convenientBanner;
    EdgeEffect leftEdge;
    EdgeEffect rightEdge;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.invasionStatusBar(this);

        setContentView(R.layout.activity_guide);
        final ArrayList<Integer> images = new ArrayList<>();
        images.add(R.mipmap.guide_1);
        images.add(R.mipmap.guide_2);
        images.add(R.mipmap.guide_3);
//        images.add(R.mipmap.guide_4);
        convenientBanner.setCanLoop(false);
//        convenientBanner.setPageIndicator(new int[]{R.drawable.dot_2_normal, R.drawable.dot_1_highlight});
        convenientBanner.setOnPageChangeListener(new SimplPageChangeListener() {
            @Override
            public void onPageScrollStateChanged(int state) {
                if (rightEdge != null && !rightEdge.isFinished()) {//到了最后一张并且还继续拖动，出现蓝色限制边条了
                    startMain();
                }
            }
        });
        convenientBanner.setOnItemClickListener(position -> {
            if (images.size() - 1 == position) {
                startMain();
            }
        });
        convenientBanner.setPages(new CBViewHolderCreator() {
            @Override
            public Holder createHolder() {
                return new Holder<Integer>() {
                    private ImageView view;

                    @Override
                    public View createView(Context context) {
                        view = new ImageView(context);
                        view.setScaleType(ImageView.ScaleType.FIT_XY);
                        return view;
                    }

                    @Override
                    public void UpdateUI(Context context, int position, Integer data) {
                        Glide.with(GuideActivity.this).load(data).into(view);
                    }
                };
            }
        }, images);
        try {
            CBLoopViewPager pager = convenientBanner.getViewPager();
            Field leftEdgeField = pager.getClass().getSuperclass().getDeclaredField("mLeftEdge");
            Field rightEdgeField = pager.getClass().getSuperclass().getDeclaredField("mRightEdge");
            if (leftEdgeField != null && rightEdgeField != null) {
                leftEdgeField.setAccessible(true);
                rightEdgeField.setAccessible(true);
                leftEdge = (EdgeEffect) leftEdgeField.get(pager);
                rightEdge = (EdgeEffect) rightEdgeField.get(pager);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void startMain() {
        Bundle bundle = new Bundle();
        bundle.putString(Key.URL, String.format("%s%s?isInit=1", ApiHelper.getHost(), Key.HTML_LOGIN));
        goActicityFinish(WebViewActivity.class, bundle);
    }

}
